<template>
  <div class="chat-container" :class="{ 'dark-theme': isDarkTheme }">
    <!-- 左侧边栏 -->
    <div class="sidebar" :class="{ collapsed: isSidebarCollapsed }">
      <div class="sidebar-header">
        <h1 class="app-title">CHAT A.I+</h1>
        <div class="toggle-sidebar" @click="toggleSidebar">
          <i :class="isSidebarCollapsed ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"></i>
        </div>
      </div>
      
      <!-- 新建聊天按钮 -->
      <div class="new-chat-btn" @click="createNewChat">
        <i class="el-icon-plus"></i>
        <span>开启新对话</span>
      </div>
      
      <!-- 对话历史 -->
      <div class="chat-history">
        <h3 class="history-title">最近对话</h3>
        <div class="clear-all" @click="clearAllChats">清除历史对话</div>
        
        <div 
          v-for="(chat, index) in chatHistory" 
          :key="index" 
          class="history-item"
          :class="{ 'active': currentChatIndex === index }"
          @click="selectChat(index)"
        >
          <i class="el-icon-chat-line-round"></i>
          <span class="chat-title">{{ chat.title }}</span>
        </div>

        <div class="last-days">
          <div class="days-label">近7天对话</div>
        </div>
      </div>
      
      <!-- 底部设置 -->
      <div class="sidebar-footer">
        <div class="settings-btn">
          <i class="el-icon-setting"></i>
          <span>设置</span>
        </div>
        
        <!-- 主题切换 -->
        <div class="theme-toggle" @click="toggleTheme" v-show="!isSidebarCollapsed">
          <i :class="isDarkTheme ? 'el-icon-moon-night' : 'el-icon-sunrise-1'"></i>
        </div>
      </div>
    </div>
    
    <!-- 右侧聊天区域 -->
    <div class="chat-area">
      <!-- 聊天头部信息 -->
      <!-- <div class="chat-header">
        <h2>{{ currentChat ? currentChat.title : 'New Chat' }}</h2>
      </div> -->
      
      <!-- 消息列表 -->
      <div class="messages-container" ref="messagesContainer">
        <div v-if="!currentChat || currentChat.messages.length === 0" class="empty-chat">
          <div class="welcome-message">
            <h2>欢迎使用 AI 聊天助手</h2>
            <p>开始提问以获取帮助</p>
          </div>
        </div>
        
        <div v-else>
          <div 
            v-for="(message, msgIndex) in currentChat.messages" 
            :key="msgIndex" 
            class="message"
            :class="{ 'user-message': message.role === 'user', 'ai-message': message.role === 'ai' }"
          >
            <!-- AI消息布局 -->
            <template v-if="message.role === 'ai'">
              <div class="message-avatar">
                <div class="ai-avatar">
                  <svg viewBox="0 0 32 32" width="24" height="24" fill="white">
                    <path d="M16,4C10.5,4,6,8.5,6,14c0,1.6,0.4,3.1,1.1,4.4c0.3,0.5,0.5,1.1,0.5,1.7v4.7c0,2,1.3,3.8,3.2,4.1c1.2,0.2,2.3-0.1,3.2-0.8c0.8-0.7,1.3-1.7,1.4-2.8l0.1-0.8c0-0.2,0.2-0.4,0.4-0.4c0,0,0,0,0.1,0c0.2,0,0.4,0.2,0.4,0.4l0.1,0.8c0.1,1.1,0.5,2.1,1.4,2.8c0.8,0.7,1.9,1,3.2,0.8c1.8-0.4,3.2-2.2,3.2-4.1V20c0-0.6,0.2-1.1,0.5-1.7C24.6,17.1,25,15.6,25,14C25,8.5,20.5,4,16,4z M14,18c-1.1,0-2-0.9-2-2s0.9-2,2-2s2,0.9,2,2S15.1,18,14,18z M18,18c-1.1,0-2-0.9-2-2s0.9-2,2-2s2,0.9,2,2S19.1,18,18,18z"></path>
                  </svg>
                </div>
              </div>
              <div class="message-content">
                <!-- <div class="message-text">{{ message.content }}</div> -->
                <div class="message-text markdown-body" v-html="renderAIMessage(message.content)"></div>

                <!-- AI消息的操作按钮 -->
                <div class="message-actions">
                  <span class="action-btn">
                    <i class="el-icon-top"></i>
                  </span>
                  <span class="action-btn">
                    <i class="el-icon-bottom"></i>
                  </span>
                  <span class="action-btn">
                    <i class="el-icon-document-copy"></i>
                  </span>
                </div>
              </div>
            </template>
            
            <!-- 用户消息布局 -->
            <template v-else>
              <div class="message-content user-content">
                <div class="message-text">{{ message.content }}</div>
              </div>
              <div class="message-avatar">
                <div class="user-avatar">U</div>
              </div>
            </template>
          </div>
        </div>
      </div>
      
      <!-- 输入区域 -->
      <div class="input-container">
        <div class="input-box" :class="{ 'ai-typing': isTyping }">
          <div class="input-emoji">😊</div>
          <textarea 
            v-model="userInput" 
            @keydown.enter="handleEnterKey"
            placeholder="What's in your mind?..."
            ref="inputField"
            rows="3"
            :disabled="isTyping"
          ></textarea>
          <div class="send-btn" :class="{ 'disabled': isTyping }" @click="sendMessage">
            <i class="el-icon-s-promotion"></i>
          </div>
          <!-- 添加AI正在输入的提示 -->
          <div class="typing-indicator" v-if="isTyping">AI正在回复，请等待...</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatInterface',
  data() {
    return {
      isDarkTheme: false,
      userInput: '',
      isTyping: false,
      currentChatIndex: null,
      typingInterval: null,
      isSidebarCollapsed: false,
      chatHistory: [],
      markdownIt: null,
      hljs: null,
      currentTypingChatId: null,
    }
  },
  computed: {
    currentChat() {
      return this.currentChatIndex !== null ? this.chatHistory[this.currentChatIndex] : null;
    }
  },
  methods: {
    toggleTheme() {
      this.isDarkTheme = !this.isDarkTheme;
    },
    
    createNewChat() {
      // 不再直接调用clearTypingInterval，而是检查是否应该保留当前的生成过程
      
      // 检查是否已有一个空的新对话
      const existingEmptyChat = this.chatHistory.findIndex(chat => chat.messages.length === 0);
      
      if (existingEmptyChat !== -1) {
        // 切换到空对话
        this.currentChatIndex = existingEmptyChat;
        this.$nextTick(() => {
          this.$refs.inputField.focus();
        });
        return;
      }

      const newChat = {
        id: Date.now(),
        title: '新对话',
        messages: []
      };
      this.chatHistory.unshift(newChat);
      this.currentChatIndex = 0;
      this.$nextTick(() => {
        this.$refs.inputField.focus();
      });
    },
    
    selectChat(index) {
      // 切换聊天，但不中断正在生成的响应
      // 保存当前正在生成的聊天ID
      this.currentChatIndex = index;
      this.$nextTick(() => {
        if (this.$refs.messagesContainer) {
          this.$refs.messagesContainer.scrollTop = this.$refs.messagesContainer.scrollHeight;
        }
      });
    },
    
    clearAllChats() {
      this.$confirm('确定要清除所有对话记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清除所有聊天时必须停止所有生成过程
        this.clearTypingInterval();
        
        this.chatHistory = [];
        this.currentChatIndex = null;

        this.$message({
          type: 'success',
          message: '清除成功!'
        });
      }).catch(() => {});
    },
    
    sendMessage() {
      if (!this.userInput.trim()) return;
      
      // 如果没有当前聊天，创建一个新的
      if (this.currentChatIndex === null) {
        this.createNewChat();
      }
      
      // 保留用户输入的原始格式
      const userContent = this.userInput.trim();
      
      // 添加用户消息
      this.currentChat.messages.push({
        role: 'user',
        content: userContent
      });
      
      // 更新聊天标题（如果是第一条消息）
      if (this.currentChat.messages.length === 1) {
        // 使用第一行作为标题
        const firstLine = userContent.split('\n')[0];
        this.currentChat.title = firstLine.substring(0, 30) + (firstLine.length > 30 ? '...' : '');
      }
      
      const userMessage = this.userInput.trim();
      this.userInput = '';
      
      // 滚动到底部
      this.$nextTick(() => {
        if (this.$refs.messagesContainer) {
          this.$refs.messagesContainer.scrollTop = this.$refs.messagesContainer.scrollHeight;
        }
      });
      
      // 模拟AI正在输入
      this.isTyping = true;
      
      // 假设一个模拟的回复，实际应用中应该调用API
      setTimeout(() => {
        this.simulateAIResponse(userMessage);
      }, 500);
    },
    
    simulateAIResponse(userMessage) {
      // 如果已经有在生成的响应，先清除
      if (this.typingInterval) {
        clearInterval(this.typingInterval);
        this.typingInterval = null;
      }
      
      // 这里应该是调用API的地方，这里仅用于演示
      const response = this.generateAIResponse(userMessage);
      const fullResponse = response;
      
      // 记录当前聊天ID，防止中途切换导致错误
      this.currentTypingChatId = this.currentChat.id;
      
      // 添加一个空的AI消息
      this.currentChat.messages.push({
        role: 'ai',
        content: ''
      });
      
      // 获取刚刚添加的消息的引用
      const messageIndex = this.currentChat.messages.length - 1;
      // 保存聊天引用，避免后续访问this.currentChat时出现问题
      const currentChatRef = this.currentChat;
      
      // 模拟流式输出效果
      let currentIndex = 0;
      this.isTyping = true;
      this.typingInterval = setInterval(() => {
        // 每次添加一个字符
        currentIndex++;
        
        // 使用保存的引用直接访问消息，而不是通过this.currentChat
        if (currentChatRef && currentChatRef.messages && currentChatRef.messages[messageIndex]) {
          currentChatRef.messages[messageIndex].content = fullResponse.substring(0, currentIndex);
          
          // 只有当用户仍在查看这个聊天时才滚动
          if (this.currentChat && this.currentChat.id === currentChatRef.id) {
            this.$nextTick(() => {
              const messagesContainer = this.$refs.messagesContainer;
              if (messagesContainer) {
                const messageElements = messagesContainer.querySelectorAll('.message');
                if (messageElements.length > 0) {
                  messageElements[messageElements.length - 1].scrollIntoView({ behavior: 'smooth', block: 'end' });
                } else {
                  messagesContainer.scrollTop = messagesContainer.scrollHeight;
                }
              }
            });
          }
        }
        
        // 全部输出完毕，清除定时器
        if (currentIndex >= fullResponse.length) {
          clearInterval(this.typingInterval);
          this.typingInterval = null;
          this.isTyping = false;
          this.currentTypingChatId = null;
        }
      }, 20); // 每20ms输出一个字符
    },
    
    generateAIResponse(message) {
      // 简单的响应生成逻辑，实际应用中应该调用LLM API
      const responses = {
        'hello': '你好！有什么可以帮助你的吗？',
        'hi': '你好！请问有什么问题吗？',
        'how are you': '我很好，谢谢询问！我是一个AI助手，随时准备帮助你。',
        'markdown': '# Markdown 示例\n\n这是一个 **Markdown** 格式的响应示例。\n\n## 功能列表\n\n- 支持标题\n- 支持加粗、*斜体*\n- 支持列表\n- 支持`代码`\n\n```javascript\nfunction example() {\n  console.log("Hello, Markdown!");\n}\n```\n\n> 引用文本效果\n\n详情请参考 [Markdown语法](https://www.markdownguide.org/)',
        'code': '```javascript\n// 代码高亮示例\nfunction calculateFactorial(n) {\n  if (n === 0 || n === 1) {\n    return 1;\n  }\n  return n * calculateFactorial(n - 1);\n}\n\nconst result = calculateFactorial(5);\nconsole.log(`5! = ${result}`);\n```\n\n你也可以尝试其他语言的代码高亮，如：\n```python\ndef fibonacci(n):\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\nprint(fibonacci(10))\n```',
        'table': '## 数据表格示例\n\n| 姓名 | 年龄 | 职业 |\n|------|------|------|\n| 张三 | 28 | 工程师 |\n| 李四 | 32 | 设计师 |\n| 王五 | 45 | 产品经理 |\n\n表格可以用于展示结构化数据。',
        'default': '感谢您的提问。作为支持Markdown的AI助手，我可以为您提供格式化的回答。\n\n## 您可以尝试以下提示词：\n\n- `markdown` - 查看Markdown样例\n- `code` - 查看代码高亮样例\n- `table` - 查看表格样例\n\n或者直接告诉我您需要了解的内容，我会尽力提供帮助。'
      };
      
      const lowerMessage = message.toLowerCase();
      
      // 检查简单的匹配
      for (const key in responses) {
        if (lowerMessage.includes(key)) {
          return responses[key];
        }
      }
      
      // 默认响应
      return responses.default;
    },
    
    handleEnterKey(e) {
      // 如果按下了Shift键，不阻止默认行为，允许换行
      if (e.shiftKey) {
        return;
      }
      
      // 如果只按下了Enter键，阻止默认行为并发送消息
      e.preventDefault();
      this.sendMessage();
    },

    // 添加新方法：将AI消息渲染为Markdown
    renderAIMessage(content) {
      return this.markdownIt.render(content);
    },
    
    toggleSidebar() {
      this.isSidebarCollapsed = !this.isSidebarCollapsed;
    },

    // 动态加载脚本的方法
    loadScript(src) {
      return new Promise((resolve, reject) => {
        const script = document.createElement('script');
        script.src = src;
        script.onload = resolve;
        script.onerror = reject;
        document.head.appendChild(script);
      });
    },

    // 修改清理定时器的方法，只用于必须终止生成的场景
    clearTypingInterval() {
      if (this.typingInterval) {
        clearInterval(this.typingInterval);
        this.typingInterval = null;
        this.isTyping = false;
        this.currentTypingChatId = null;
      }
    },
  },
  async mounted() {
    // 动态加载 highlight.js 和 markdown-it
    await this.loadScript('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js');
    await this.loadScript('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/languages/go.min.js');
    await this.loadScript('https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it.min.js');

    // 确保全局变量存在
    if (typeof hljs === 'undefined' || typeof markdownit === 'undefined') {
      console.error('Failed to load highlight.js or markdown-it');
      return;
    }

    // 初始化 markdown-it 并配置 highlight.js
    this.hljs = window.hljs;
    this.markdownIt = window.markdownit({
      html: true,
      highlight: (str, lang) => {
        if (lang && this.hljs.getLanguage(lang)) {
          try {
            return `<pre><code class="hljs ${lang}">${this.hljs.highlight(lang, str, true).value}</code></pre>`;
          } catch (error) {
            console.error('Error highlighting code:', error);
          }
        }

        return `<pre><code class="hljs">${this.markdownIt.utils.escapeHtml(str)}</code></pre>`;
      }
    });

    // 手动触发 highlight.js 的高亮
    this.$nextTick(() => {
      this.hljs.highlightAll();
    });

    // 默认选择第一个聊天
    if (this.chatHistory.length > 0) {
      this.currentChatIndex = 0;
    }
  }
}
</script>

<style scoped>
/* 基础样式 */
.chat-container {
  display: flex;
  height: 100vh;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  color: #333;
  background-color: #f5f5f5;
  transition: all 0.3s ease;
}

/* 深色主题 */
.dark-theme {
  background-color: #1a1a1a;
  color: #f5f5f5;
}

.dark-theme .sidebar {
  background-color: #252525;
  border-right: 1px solid #333;
}

.dark-theme .chat-area {
  background-color: #1a1a1a;
}

.dark-theme .input-box {
  background-color: #333;
  border: 1px solid #444;
}

.dark-theme .input-box textarea {
  background-color: #333;
  color: #f5f5f5;
}

.dark-theme .message.user-message {
  background-color: transparent;
}

.dark-theme .message.ai-message {
  background-color: transparent;
}

.dark-theme .message.ai-message .message-content {
  background-color: #252525;
}

.dark-theme .new-chat-btn,
.dark-theme .history-item {
  background-color: #333;
  color: #f5f5f5;
}

.dark-theme .history-item:hover {
  background-color: #444;
}

.dark-theme .history-item.active {
  background-color: #4a4a4a;
}

/* 侧边栏样式 */
.sidebar {
  width: 280px;
  background-color: #fff;
  border-right: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.3s ease;
}

.sidebar.collapsed {
  width: 60px;
}

.sidebar.collapsed .app-title, 
.sidebar.collapsed .chat-title,
.sidebar.collapsed .history-title,
.sidebar.collapsed .clear-all,
.sidebar.collapsed .settings-btn span,
.sidebar.collapsed .new-chat-btn span,
.sidebar.collapsed .days-label,
.sidebar.collapsed .search-box {
  display: none;
}

.sidebar.collapsed .new-chat-btn,
.sidebar.collapsed .history-item,
.sidebar.collapsed .sidebar-footer {
  justify-content: center;
}

.sidebar.collapsed .sidebar-header {
  padding: 15px 10px;
}

.sidebar.collapsed .toggle-sidebar {
  margin: 0 auto;
}

.sidebar.collapsed .chat-history {
  padding: 0 5px;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid #eaeaea;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.app-title {
  font-size: 22px;
  font-weight: bold;
  margin: 0;
}

.new-chat-btn {
  margin: 12px;
  padding: 10px 15px;
  background-color: #4c6ef5;
  color: white;
  border-radius: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.new-chat-btn:hover {
  background-color: #3b5cde;
}

.search-box {
  margin: 0 12px;
  position: relative;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #777;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px;
}

.history-title {
  font-size: 14px;
  color: #666;
  margin: 15px 0 10px;
  display: inline-block;
}

.clear-all {
  float: right;
  font-size: 12px;
  color: #4c6ef5;
  margin-top: 15px;
  cursor: pointer;
}

.history-item {
  padding: 10px 15px;
  margin: 5px 0;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background-color 0.2s;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.history-item:hover {
  background-color: #f0f0f0;
}

.history-item.active {
  background-color: #e6e6fa;
}

.chat-title {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.last-days {
  margin-top: 15px;
  padding-top: 10px;
  border-top: 1px solid #eaeaea;
}

.days-label {
  font-size: 12px;
  color: #888;
  margin-bottom: 10px;
}

.sidebar-footer {
  padding: 15px;
  border-top: 1px solid #eaeaea;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.settings-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: #666;
}

.theme-toggle {
  cursor: pointer;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.theme-toggle:hover {
  background-color: #f0f0f0;
}

/* 聊天区域样式 */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  height: 100vh;
}

.chat-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eaeaea;
}

.chat-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.input-container,
.messages-container {
  padding-left: 20px;
  padding-right: 20px;
}

.input-container {
  padding-top: 15px;
  padding-bottom: 15px;
  /* border-top: 1px solid #eaeaea; */
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding-top: 20px;
  padding-bottom: 20px;
  display: flex;
  flex-direction: column;
  -webkit-overflow-scrolling: touch; /* 为iOS设备提供更平滑的滚动 */
  overscroll-behavior: contain; /* 防止滚动传递给父元素 */
  scroll-behavior: smooth; /* 平滑滚动 */
  height: calc(100vh - 120px); /* 减去输入框高度和可能的padding */
}

.empty-chat {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #888;
}

.welcome-message {
  text-align: center;
}

.welcome-message h2 {
  font-size: 24px;
  margin-bottom: 10px;
}

.message {
  display: flex;
  margin-bottom: 20px;
  width: 100%;
}

.message-avatar {
  margin-right: 12px;
  min-width: 36px;
}

.user-message {
  justify-content: flex-end;
}

.ai-message {
  justify-content: flex-start;
}

.user-message .message-content {
  max-width: max-content;
}

.ai-message .message-content {
  max-width: 70%;
}

.user-message .message-avatar {
  margin-right: 0;
  margin-left: 12px;
}

.user-content {
  text-align: left;
  background-color: #4361ee !important;
  color: white;
}

.user-avatar, .ai-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.user-avatar {
  background-color: #4c6ef5;
  color: white;
}

.ai-avatar {
  background-color: #5e72e4;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.message-content {
  flex: 1;
  padding: 15px;
  border-radius: 10px;
  position: relative;
}

.user-message .message-content {
  background-color: #f0f0f0;
}

.ai-message .message-content {
  background-color: #f9f9f9;
}

.message-text {
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: none;
  /*font-family: monospace;*/ /* 使用等宽字体以保持代码格式对齐 */
}

.message-actions {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.action-btn {
  cursor: pointer;
  color: #777;
  transition: color 0.2s;
}

.action-btn:hover {
  color: #4c6ef5;
}

/* 输入区域样式 */
.input-box {
  display: flex;
  align-items: flex-start;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 20px;
  padding: 10px 15px;
  position: relative; /* 添加相对定位，用于放置AI输入提示 */
  transition: all 0.3s ease; /* 添加过渡效果 */
}

/* AI正在输入时的样式 */
.input-box.ai-typing {
  border-color: #ddd;
  background-color: #f9f9f9;
  opacity: 0.9;
}

/* AI正在输入的提示 */
.typing-indicator {
  position: absolute;
  top: -30px;
  left: 15px;
  font-size: 12px;
  color: #ff6b6b;
  background-color: #fff8f8;
  padding: 5px 10px;
  border-radius: 10px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  animation: pulse 1.5s infinite;
  z-index: 10;
}

@keyframes pulse {
  0% { opacity: 0.6; }
  50% { opacity: 1; }
  100% { opacity: 0.6; }
}

.dark-theme .typing-indicator {
  background-color: #3a3a3a;
  color: #ff8080;
}

.input-emoji {
  font-size: 18px;
  margin-right: 10px;
  cursor: pointer;
  padding-top: 5px;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.ai-typing .input-emoji {
  opacity: 0.5;
}

.input-box textarea {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  height: 72px;
  min-height: 72px;
  font-family: inherit;
  font-size: 14px;
  padding: 5px 0;
  line-height: 1.5;
  transition: all 0.3s ease; /* 添加过渡效果 */
}

/* 禁用状态的输入框样式 */
.input-box textarea:disabled {
  color: #999;
  cursor: not-allowed;
  background-color: transparent; /* 更改为透明背景，保持视觉一致性 */
  opacity: 0.8;
}

.dark-theme .input-box textarea:disabled {
  background-color: transparent; /* 暗黑模式也使用透明背景 */
  color: #777;
}

.send-btn {
  cursor: pointer;
  margin-left: 10px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: #4c6ef5;
  color: white;
  transition: all 0.3s ease; /* 改为all包含所有属性 */
  margin-top: 5px;
}

/* 禁用状态的发送按钮样式 */
.send-btn.disabled {
  background-color: #c0c0c0; /* 改为更柔和的禁用色 */
  cursor: not-allowed;
  opacity: 0.7; /* 添加透明度效果 */
}

.dark-theme .send-btn.disabled {
  background-color: #444;
  opacity: 0.7;
}

.send-btn:hover:not(.disabled) {
  background-color: #3b5cde;
}

/* 适配不同屏幕大小 */
@media (max-width: 768px) {
  .chat-container {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    height: 50px;
    flex-direction: row;
    align-items: center;
    padding: 0 15px;
  }
  
  .chat-history, .sidebar-footer {
    display: none;
  }
  
  .app-title {
    font-size: 18px;
  }
  
  .new-chat-btn {
    margin: 0 10px;
    padding: 5px 10px;
  }
  
  .user-message {
    padding-left: 10%;
  }
  
  .ai-message {
    padding-right: 10%;
  }
}

.toggle-sidebar {
  cursor: pointer;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.toggle-sidebar:hover {
  background-color: #f0f0f0;
}

.dark-theme .toggle-sidebar:hover {
  background-color: #444;
}

/* 添加Markdown样式 */
.markdown-body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  line-height: 1.6;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-body h1 {
  font-size: 2em;
  border-bottom: 1px solid #eaecef;
  padding-bottom: 0.3em;
}

.markdown-body h2 {
  font-size: 1.5em;
  border-bottom: 1px solid #eaecef;
  padding-bottom: 0.3em;
}

.markdown-body h3 {
  font-size: 1.25em;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 2em;
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-body blockquote {
  margin: 0;
  padding: 0 1em;
  color: #6a737d;
  border-left: 0.25em solid #dfe2e5;
}

.markdown-body pre {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f6f8fa;
  border-radius: 3px;
}

.markdown-body code {
  padding: 0.2em 0.4em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(27, 31, 35, 0.05);
  border-radius: 3px;
}

.markdown-body pre > code {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: 16px;
}

.markdown-body table th,
.markdown-body table td {
  padding: 6px 13px;
  border: 1px solid #dfe2e5;
}

.markdown-body table tr {
  background-color: #fff;
  border-top: 1px solid #c6cbd1;
}

.markdown-body table tr:nth-child(2n) {
  background-color: #f6f8fa;
}

.markdown-body img {
  max-width: 100%;
  box-sizing: content-box;
}

.dark-theme .markdown-body blockquote {
  border-left-color: #4a4a4a;
  color: #a0a0a0;
}

.dark-theme .markdown-body pre {
  background-color: #2d2d2d;
}

.dark-theme .markdown-body code {
  background-color: rgba(255, 255, 255, 0.1);
}

.dark-theme .markdown-body table th,
.dark-theme .markdown-body table td {
  border-color: #444;
}

.dark-theme .markdown-body table tr {
  background-color: #2a2a2a;
  border-top-color: #444;
}

.dark-theme .markdown-body table tr:nth-child(2n) {
  background-color: #333;
}

.dark-theme .markdown-body h1,
.dark-theme .markdown-body h2 {
  border-bottom-color: #444;
}

.dark-theme .input-box.ai-typing {
  border-color: #252525;
  background-color: #252525;
  opacity: 0.9;
}

/* 引入 highlight.js 的默认样式 */
/* @import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/default.min.css'); */
/* @import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/github.min.css'); */
/* @import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/github-dark.min.css'); */
/* @import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/monokai-sublime.min.css'); */
/* @import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/solarized-dark.min.css'); */
@import url('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/atom-one-dark.min.css');

/* 代码块容器的样式 */
.code-block-wrapper {
  position: relative;
  margin: 1em 0;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: #f5f5f5;
}

/* 代码块头部的样式 */
.code-block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px;
  background: #eee;
  border-bottom: 1px solid #ddd;
}

/* 语言标签的样式 */
.language-label {
  font-size: 0.9em;
  color: #555;
}

/* 复制按钮的样式 */
.copy-button {
  background: #007bff;
  color: white;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8em;
}

.copy-button:hover {
  background: #0056b3;
}

/* 代码块的样式 */
pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
}

::v-deep .hljs {
  border-radius: 18px;
}
</style> 